package com.tigerhhzz.service;

import com.aliyun.teaopenapi.Client;
import com.aliyun.teaopenapi.models.Config;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @Author tigerhhzz
 * @Date 2024 10 05 11 40
 **/
@Service
public class SmsService {

    @Value("${aliyun.sms.accessKey}")
    private String accessKeyId;

    @Value("${aliyun.sms.regionId}")
    private String regionId;

    @Value("${aliyun.sms.domain}")
    private String domain;

    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.sms.signName}")
    private String signName;

    @Value("${aliyun.sms.templateCode}")
    private String templateCode;

    private final JdbcTemplate jdbcTemplate;
    private final VerificationCodeService verificationCodeService;

    public SmsService(JdbcTemplate jdbcTemplate, VerificationCodeService verificationCodeService) {
        this.jdbcTemplate = jdbcTemplate;
        this.verificationCodeService = verificationCodeService;
    }

    public String sendSms(String phoneNumber, String uniqueId) {
        try {
            // 生成验证码
            String verificationCode = verificationCodeService.generateVerificationCode(phoneNumber);


            DefaultProfile profile = DefaultProfile.getProfile(regionId,accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain(domain);
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", regionId);
            request.putQueryParameter("PhoneNumbers", phoneNumber); //目标手机号
            request.putQueryParameter("SignName", signName); //签名名称
            request.putQueryParameter("TemplateCode", templateCode); //短信模板code
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + verificationCode + "\"}");//模板中变量替换

            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            System.out.println("发送短信后的响应结果："+data);
            if (StringUtils.contains(data, "\"Message\":\"OK\"")) {
                // 发送成功，更新数据库状态
                jdbcTemplate.update("UPDATE sms_log SET send_status = 1, send_time = NOW() WHERE unique_id =?", uniqueId);
                return "发送成功，验证码："+verificationCode;
            } else {
                // 发送失败，也可以记录失败原因等
                jdbcTemplate.update("UPDATE sms_log SET send_status = 0 WHERE unique_id =?", uniqueId);
                return "发送失败，验证码："+verificationCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 发送异常，更新数据库状态为失败
            jdbcTemplate.update("UPDATE sms_log SET send_status = 0 WHERE unique_id =?", uniqueId);
            System.out.println("发送短信验证码失败~ phoneNumber = " + phoneNumber + e);
        }
        return null;
    }
}
